package com.xc.quickstart.common.util;

import cn.hutool.core.convert.Convert;
import com.xc.quickstart.common.context.CommonContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.awt.geom.Point2D;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
public class CommonUtil {

    public static String getHideText(String name, String phone, String secondName, String secondPhone) {
        if (StringUtils.isNotEmpty(name)) {
            name = getHideName(name);
        }

        if (StringUtils.isNotEmpty(phone)) {
            phone = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }

        if (StringUtils.isNotEmpty(secondName)) {
            secondName = getHideName(secondName);
        }

        if (StringUtils.isNotEmpty(secondPhone)) {
            secondPhone = secondPhone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }

        String first = Stream.of(name, phone).filter(StringUtils::isNotEmpty).collect(Collectors.joining(","));

        String second = Stream.of(secondName, secondPhone).filter(StringUtils::isNotEmpty).collect(Collectors.joining(","));

        if (StringUtils.isNotEmpty(second)) {
            first += (";" + second);
        }
        return first;
    }

    public static String getHideName(String name) {
        String first = name.substring(0, 1);
        if (name.length() < 2) {
            return name;
        }

        if (name.length() == 2) {
            return first + "*";
        }

        String end = name.substring(name.length() - 1);
        first = first + "*" + end;
        return first;
    }

    public static String encryptName(String name){
        if (StringUtils.isEmpty(name)) {
            return name;
        }

        String encryptName;
        String first = name.substring(0, 1);

        if(name.length() < 2){
            encryptName = name;
        }else if(name.length() == 2){
            encryptName = first + "*";
        }else {
            encryptName  = first + "*" + name.substring(name.length()-1);
        }
        return encryptName;
    }

    public static String encryptPhone(String phone) {
        if (StringUtils.isNotEmpty(phone) && phone.length() >= 11) {
            return phone.substring(0, 3) + "****" + phone.substring(phone.length() - 4);
        }
        return phone;
    }

    public static String getDistanceDesc(double distance) {
        if (distance == 0) {
            return "0米";
        }
        if (distance <= 1000.0) {
            return Convert.toBigDecimal(distance).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue() + "m";
        } else {
            return Convert.toBigDecimal(distance / 1000.0).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue() + "km";
        }
    }

    public static List<Point2D.Double> dealWithShape(String shape) {
        if (StringUtils.isEmpty(shape)) {
            return Collections.emptyList();
        }
        return Arrays.stream(shape.split(CommonContext.SEMICOLON)).map(location -> {
            String[] split = location.split(CommonContext.COMMA);
            return new Point2D.Double(Double.parseDouble(split[0]), Double.parseDouble(split[1]));
        }).collect(Collectors.toList());
    }

    public static String dealWithDistance(double distance) {
        if (distance <= CommonContext.ROUND_NUM) {
            return BigDecimal.valueOf(distance).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue() + CommonContext.METER;
        } else {
            return BigDecimal.valueOf(distance / CommonContext.ROUND_NUM).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue() + CommonContext.KILO_METER;
        }
    }
}
